package com.zhangjian.test1;

public class synchronizedTest {
    private  char[] date1 =  {'1','2','3','4','5','6','7'};
    private  char[] date2 =  {'a','b','c','d','e','f','g'};
    private  Object object = new Object();
    private  void clock(){
        Thread thread1 = new Thread(
                () ->{
                    synchronized (object) {
                        for (char temp : date1) {
                            System.out.print(temp);
                            object.notify();//随机唤醒一个在object上等待的线程
                            try {
                                object.wait();//是当前线程等待，定释放锁
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        object.notify();
                    }
                }
        );
        Thread thread2 = new Thread(
                () ->{
                    synchronized (object) {
                        for (char temp : date2) {
                            System.out.print(temp);
                            object.notify();
                            try {
                                object.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        object.notify();
                    }
                }
        );
        thread1.start();
        thread2.start();
    }

    public static void main(String[] args) {
        new synchronizedTest().clock();
    }
}
